using System.Collections.Generic;
using Drill.Configuration;

namespace Drill.Core
{
	public class ConfigBasedConfigurationSourceSetFactory : IConfigurationSourceSetFactory
	{
		private readonly IConfigSourceConfigurationCollection _configSourceConfigs;
		private readonly IConfigurationHelper _configurationHelper;

		public ConfigBasedConfigurationSourceSetFactory(
			IConfigSourceConfigurationCollection configSourceConfigs = null,
			IConfigurationHelper configurationHelper = null
			)
		{
			_configSourceConfigs = configSourceConfigs;
			_configurationHelper = configurationHelper;
		}

		public IList<IConfigurationSource> CreateConfigurationSources()
		{
			List<IConfigurationSource> configSources = new List<IConfigurationSource>();

			if (null != _configSourceConfigs && _configSourceConfigs.Count > 0)
			{
				foreach (IConfigSourceConfiguration configSourceConfig in _configSourceConfigs)
				{
					if (configSourceConfig.Enabled)
					{
						IConfigurationSourceFactory factory = 
							new ConfigBasedConfigurationSourceFactory(configSourceConfig, _configurationHelper);

						IConfigurationSource configSource = factory.CreateConfigurationSource();
						if (null == configSource)
						{
							continue;
						}
						configSources.Add(configSource);
					}
				}
			}
			return configSources;
		}
	}
}